// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var validate = require("validate");
	var css_param = require("./createPickup1.css");
	var tools = require("tools");
	var databind = require("databind");
	var constant = require("constant");
    var html_bulkstockInfo = require("./html_bulkstockInfo_2.html");//查询货物弹出框内容
    var html_commonlyInfo = require("./html_commonlyInfo.html");
    var router = require("../hash/pageRouter");
    var ds;
    var self=true;
    var enterpriseName;
    var createPickups=6;
    var pageIndex = 1,
		pageSize = 10,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var init = function() {
		router.init();
		inquireFinanceApplicationDetail();
		initEvent();
		
	};
		
		var initEvent = function() {
			
			$("#createPickup1 input[name=productType]").val(createPickups);
			//发票类型点击事件
			$("#createPickup1 input[name=invoiceType]").unbind("click").click(function(){
				if ($(this).val() == "0") {
					$("#createPickup1 .invoiceInfo1").html("");
				} else if ($(this).val() == "1"){
					var str = '<div class="dataLine">				<div class="requireInput">					公司名称:<input type="text" isNull="1" codeName="公司名称" name="invoiceEnterpriseName" placeholder="请输入公司名称"/>				</div>				<div>				</div>			</div>			<div class="dataLine">				<div class="requireInput">					纳税识别号:<input type="text" isNull="1" codeName="纳税识别号" name="invoiceTaxpayerNumber" placeholder="请输入纳税识别号"/>				</div>				<div>				</div>			</div>			<div class="dataLine">				<div class="requireInput">					地址、电话:<input type="text" isNull="1" codeName="地址、电话" name="invoiceAddress" placeholder="请输入地址、电话"/>				</div>				<div>				</div>			</div>			<div class="dataLine">				<div class="requireInput">					开户行及账号:<input type="text" isNull="1" codeName="开户行及账号" name="invoiceAccount" placeholder="请输入开户行及账号"/>				</div>				<div>				</div>			</div>';
					$("#createPickup1 .invoiceInfo1").html(str);
				} else if ($(this).val() == "2") {
					var str = '<div class="dataLine">				<div class="requireInput">					公司名称:<input type="text" isNull="1" codeName="公司名称" name="invoiceEnterpriseName" placeholder="请输入公司名称"/>				</div>				<div>				</div>			</div>			<div class="dataLine">				<div class="requireInput">					纳税识别号:<input type="text" isNull="1" codeName="纳税识别号" name="invoiceTaxpayerNumber" placeholder="请输入纳税识别号"/>				</div>				<div>				</div>			</div>';
					$("#createPickup1 .invoiceInfo1").html(str);
				}
				
			});
			
			$("#createPickup1").on("change", "input[name=others]", function(){
				var $this = $(this);
				if (!isNaN($this.parents("tr").find("input[name=bulkstockAmounts]").val()) && !isNaN($this.parents("tr").find("input[name=interests]").html()) && !isNaN($this.parents("tr").find("input[name=others]").html())) {
					$this.parents("tr").find(".pickupAmount").html($this.parents("tr").find("input[name=interests]").val()*1 + $this.parents("tr").find("input[name=others]").val()*1 + $this.parents("tr").find("input[name=bulkstockAmounts]").val()*1);
					$this.parents("tr").find("input[name=pickupAmounts]").val($this.parents("tr").find("input[name=interests]").val()*1 + $this.parents("tr").find("input[name=others]").val()*1 + $this.parents("tr").find("input[name=bulkstockAmounts]").val()*1);
				}
			});
			
			$("#createPickup1").on("change", "input[name=interests]", function(){
				var $this = $(this);
				if (!isNaN($this.parents("tr").find("input[name=bulkstockAmounts]").val()) && !isNaN($this.parents("tr").find("input[name=interests]").html()) && !isNaN($this.parents("tr").find("input[name=others]").html())) {
					$this.parents("tr").find(".pickupAmount").html($this.parents("tr").find("input[name=interests]").val()*1 + $this.parents("tr").find("input[name=others]").val()*1 + $this.parents("tr").find("input[name=bulkstockAmounts]").val()*1);
					$this.parents("tr").find("input[name=pickupAmounts]").val($this.parents("tr").find("input[name=interests]").val()*1 + $this.parents("tr").find("input[name=others]").val()*1 + $this.parents("tr").find("input[name=bulkstockAmounts]").val()*1);
				}
			});
			
			$("#createPickup1").on("change", "input[name=bulkstockAmounts]", function(){
				var $this = $(this);
				if (!isNaN($this.parents("tr").find("input[name=bulkstockAmounts]").val()) && !isNaN($this.parents("tr").find("input[name=interests]").html()) && !isNaN($this.parents("tr").find("input[name=others]").html())) {
					$this.parents("tr").find(".pickupAmount").html($this.parents("tr").find("input[name=interests]").val()*1 + $this.parents("tr").find("input[name=others]").val()*1 + $this.parents("tr").find("input[name=bulkstockAmounts]").val()*1);
					$this.parents("tr").find("input[name=pickupAmounts]").val($this.parents("tr").find("input[name=interests]").val()*1 + $this.parents("tr").find("input[name=others]").val()*1 + $this.parents("tr").find("input[name=bulkstockAmounts]").val()*1);
				}
			});
			
			$("#createPickup1 .commonlyInfo").unbind("click").click(function(){
				var ii = layer.open({
				  title: '常用信息',
				  type: 1,
				  skin: 'layui-layer-rim', //加上边框
				  area: ['1000px', '650px'], //宽高
				  content: html_commonlyInfo,
				  btn: ['确定', '取消'],
				  yes: function(){
					databind.fillForm($("#createPickup1 .invoiceInfo"), ds[$(".html_commonlyInfo input[name=selected]:checked").parents("tr").attr("index")]);
					$("#createPickup1 input[name=buyer]").val(enterpriseName.enterpriseName);
					layer.close(ii);
				  }
				});
				
				var oData = {
					pageIndex: pageIndex,
					pageSize: pageSize,
					totalPageCount: 0
				};
				
				$(".html_commonlyInfo .search").unbind("click").click(function(){
					oData = {
						pageIndex: pageIndex,
						pageSize: pageSize,
						totalPageCount: 0,
						invoiceEnterpriseName: $(".html_commonlyInfo input[name]").val()
					};
					tools.loadList("inquireEnterpriseInvoiceInfoPageList.json", $(".html_commonlyInfo .dataBody"), $(".html_commonlyInfo .dataTpl"), oData, function(data){
						ds = data.datas;
					}, $(".html_commonlyInfo .pagerCon"));
				});
				
				tools.loadList("inquireEnterpriseInvoiceInfoPageList.json", $(".html_commonlyInfo .dataBody"), $(".html_commonlyInfo .dataTpl"), oData, function(data){
					ds = data.datas;
				}, $(".html_commonlyInfo .pagerCon"));
			});
			
//			tools.loadSelectList($("#createPickup1 select[name=enterpriseId]"), "inquireEnterpriseList.json?self=true", "id", "name", null);
			
			tools.loadSelectList($("#createPickup1 select[name=enterpriseId]"), "inquireEnterpriseList.json", "id", "name", null);
			
			$("#createPickup1 .addBulkstockInfo").unbind("click").click(function(){
				var ii = layer.open({
				  title: '添加货物信息',
				  type: 1,
				  skin: 'layui-layer-rim', //加上边框
				  area: ['1000px', '600px'], //宽高
				  content: html_bulkstockInfo,
				  btn: ['确定', '取消'],
				  yes: function(){
				  	if ($(".html_bulkstockInfo .check:checked").length > 0) {
				  	 	var i = 1;
					  	$.each($(".html_bulkstockInfo .check:checked"), function() {
					  		var $this = $(this);
					  		var tr = $(this).parents("tr").clone();
					  		tr.find(".check").parent().remove();
					  		tr.find(".index").html(i);
					  		/*tr.find(".showFinanceDetail_s2").html("&nbsp;&nbsp;[+]");*/
//					  		tr.append("<td><a href='javascript:void(0);' class='delete'>删除</a></td>");
					  		
					  		tr.find(".pickupAmount").parent().append("<input type='hidden' name='pickupAmounts' disabled='disabled'/>");
					  		
					  		tr.find("input[name=pickupQuantitys]").parent().append(tr.find("input[name=pickupQuantitys]").val());
					  		
					  		$("#createPickup1 .table").append(tr);
					  		
					  		i++;
					  	});
					  	layer.close(ii);
				  	} else {
						base.alert(2, "请选择货物！");
					}
				  	
				  	tools.clearNoNumAll($("#createPickup1"));
				  	
				  	if ($("#createPickup1 .table tr").length > 1) {
						$("#createPickup1 select[name=enterpriseId]").attr("disabled", "disabled");
					} else {
						$("#createPickup1 select[name=enterpriseId]").removeAttr("disabled");
					}
				  }
				});
				
				tools.loadSelectList($(".html_bulkstockInfo select[name=bulkstockInfoId]"), "inquireBulkstockPageList.json?pageSize=1000", "bulkstockInfoId", "bulkstockName", "specificationName");
				tools.loadSelectList($(".html_bulkstockInfo select[name=warehouseName]"), "inquireWarehouseList.json?enterpriseId=" + $("#createPickup1 select[name=enterpriseId]").val(), "text", "text", null);
				$(".html_bulkstockInfo span[name=enterpriseName]").html($("#createPickup1 select[name=enterpriseId] option:selected").text());
				$(".html_bulkstockInfo input[name=financeNumber]").html($("#createPickup1 input[name=financeNumber]").text()); 
				$(".html_bulkstockInfo input[name=enterpriseId]").val($("#createPickup1 select[name=enterpriseId]").val());
				$(".html_bulkstockInfo select[name=productType]").val(createPickups)

				$(".html_bulkstockInfo .search").unbind("click").click(function(){
					databind.submitDom($(".html_bulkstockInfo"), function(data){
						juicer.register("getValueByKey", constant.getValueByKey);
						var tpl = $(".html_bulkstockInfo .dataTpl").html();
						var html = juicer(tpl, data);
						$(".html_bulkstockInfo .dataBody").html(html);
						
						$.each($(".html_bulkstockInfo .check"), function() {
							var $this1 = $(this).parents("tr");
							$.each($("#createPickup1 table tr"), function(){
								var $this2 = $(this);
								if (($this1.find("input[name=locationIds]").val() != null && $this2.find("input[name=locationIds]").val() != null) && $this1.find("input[name=locationIds]").val() == $this2.find("input[name=locationIds]").val()) {
									if (($this1.find("input[name=financeIds]").val() != null && $this2.find("input[name=financeIds]").val() != null) && $this1.find("input[name=financeIds]").val() == $this2.find("input[name=financeIds]").val()) {
										$this1.find("input").attr("disabled", "disabled");
									}
								}
							});
						});
					});
				});
				
			});
			
			$("#createPickup1").on("click", ".delete", function(){
				$(this).parents("tr").remove();
				
				if ($("#createPickup1 .table tr").length > 1) {
					$("#createPickup1 select[name=enterpriseId]").attr("disabled", "disabled");
				} else {
					$("#createPickup1 select[name=enterpriseId]").removeAttr("disabled");
				}
			});
			
			tools.fileupload($("#createPickup1 #createPickup1FilesUuid"), null, function(data){
				var str = '<span class="showFiles"><a target="_blank" class="mas" fileUuid="' + data.result.data[0].fileUuid + '" href="/finance/api/v1.0/downloadFile.file?fileUuid=' + data.result.data[0].fileUuid + '" class="showProductIntro">' + data.result.data[0].fileName + '</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class="removeFile">x</a></span>';
				$("#createPickup1 .addfile").before(str);
				var fs = [];
				$.each($('#createPickup1 .mas'), function() {
					fs.push($(this).attr("fileUuid"));
				});
				$("#createPickup1 input[name=filesUuid]").val(fs);
			});
			
			$("#createPickup1").on("click", ".removeFile", function(){
				$(this).parents(".showFiles").remove();
				var fs = [];
				$.each($('#createPickup1 .mas'), function() {
					fs.push($(this).attr("fileUuid"));
				});
				$("#createPickup1 input[name=filesUuid]").val(fs);
			});
			
			//提交
			$("#createPickup1 .sure").unbind("click").click(function(){
				if ($("#createPickup1 table tr").length > 1) {
					databind.submitDom($("#createPickup1"), function(){
						base.alert(1, "提交成功!");
						router.route("account/warehousePickupList.html");
					});
				} else {
					base.alert(2, "请先添加提货货物！");
				}
			});
			
			//取消
//			$("#createPickup1 .cancel_s1").unbind("click").click(function(){
//				router.route("account/warehousePickupList.html");
//			});
			
		};
		
		//查询企业详情
		var inquireFinanceApplicationDetail = function() {
			base.fnAjax({
				action: "inquireCurrentEnterpriseDetail.json",
				nocode: true,
				oData: {
				
				}, //传递参数
				fnSuccess: function(data) {
					if (data.status == 200) {
						enterpriseName = data.datas[0];	
						$("#createPickup1 input[name=buyer]").val(data.datas[0].enterpriseName);
						$("#createPickup1 select[name=enterpriseId]").val(data.datas[0].enterpriseId);
					}else {
						//
					}
				}
			})
		};
		
		return {
			init: init
		};
});